package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.SmsFlashPromotionMapper;
import com.mail.dubbo.mapper.SmsFlashPromotionProductRelationMapper;
import com.mail.model.db.SmsFlashPromotion;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author yan
 * @date 2022/06/22 14:11
 **/
@DubboService
public class SmsFlashPromotionApiImpl implements SmsFlashPromotionApi{
@Autowired
private SmsFlashPromotionMapper mapper;
@Autowired
private SmsFlashPromotionProductRelationMapper relationMapper;
    @Override
    public void create(SmsFlashPromotion smsFlashPromotion) {
        mapper.insert(smsFlashPromotion);
    }

    @Override
    public void delete(Long id) {
        relationMapper.deleteByFlashPromotionId(id);
        mapper.deleteById(id);
    }

    @Override
    public Page<SmsFlashPromotion> selectList(String keyword, Long pageNum, Long pageSize) {
        LambdaQueryWrapper<SmsFlashPromotion> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(keyword),SmsFlashPromotion::getTitle,keyword)
                .orderByDesc(SmsFlashPromotion::getCreateTime);
        Page<SmsFlashPromotion> page =new Page<>(pageNum,pageSize);
         mapper.selectPage(page, wrapper);
         return page;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        mapper.updateStatus(id,status);
    }

    @Override
    public void updateFlashPromotion(Long id, SmsFlashPromotion promotion) {
        mapper.updateById(promotion);
    }

    @Override
    public SmsFlashPromotion getFlashPromotion(Long id) {
        return mapper.selectById(id);
    }
}
